package com.pac.hospital.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.page.Page;
import com.common.page.PageContext;
import com.common.tools.StringTools;
import com.common.tools.TimeTools;
import com.common.util.LogicException;
import com.pac.hospital.bean.HospitalDepartmentBean;
import com.pac.hospital.service.HospitalDepartmentService;

@Controller
@Scope("prototype")
@RequestMapping("/hospitalDepartment")
public class HospitalDepartmentController {
	@Autowired
     private HospitalDepartmentService hospitalDepartmentService;
     
	/**
	 * 角色列表查询
	 * 
	 * @param userBean
	 * @param page
	 * @param rows
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Page queryUserList(HttpServletRequest request, HospitalDepartmentBean hospitalDepartmentBean,
			int page, int rows,String hospitalId) {
		// 设置每页记录数
		PageContext.getContext().setPageSize(rows);
		// 跳转到指定页
		PageContext.getContext().gotoPage(page);

		List<HospitalDepartmentBean> hospitalDepartmentBeanList = hospitalDepartmentService.queryHospitalDepartment(hospitalDepartmentBean);
             
		Page currentPage = new Page();
		// 总行数
		int totlaRols = PageContext.getContext().getTotal();
		currentPage.setTotal(totlaRols);
		currentPage.setRows(hospitalDepartmentBeanList);

		return currentPage;
	}
	/***
	 * 页面跳转
	 * <功能详细描述>
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("gotoPage")
	public ModelAndView gotoPage(String pageName) {
		ModelAndView mov = new ModelAndView();
		mov.setViewName("hospital/" + pageName);
		return mov;
	 }
	
	/***
	 * 页面跳转/进入添加医院界面
	 * <功能详细描述>
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("add")
	public ModelAndView add(){
		ModelAndView mov = new ModelAndView();
		mov.setViewName("hospital/hospitalAdd");
		return mov;
		
	}
	/***
	 * 保存科室
	 * <功能详细描述>
	 * @param hospitalDepartmentBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	 @RequestMapping("save")
	 @ResponseBody
     public Map<String, Object> save(HospitalDepartmentBean hospitalDepartmentBean){
			boolean result = true;
			String msg = "";
			
		String 	hospitalId= hospitalDepartmentBean.getHospitalId();
		if (!StringTools.isNull(hospitalId)) {
			hospitalDepartmentBean.setCreateDate(TimeTools.now());
			hospitalDepartmentService.addHospitalDepartment(hospitalDepartmentBean);
			msg = "新增用户【" + hospitalDepartmentBean.getName() + "】成功";
		}else{
			msg = "新增用户出错，医院不能为空";

			result=false;
			
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);
		
			return map;
					 
	 }
	 /**
	  * 修改科室信息
	  * <功能详细描述>
	  * @param hospitalDepartmentBean
	  * @return
	  * @see [类、类#方法、类#成员]
	  */
		@RequestMapping("/edit")
		@ResponseBody
		public Map<String, Object> edit(HospitalDepartmentBean hospitalDepartmentBean) {
			boolean result = true;
			String msg = "";
         try{
			hospitalDepartmentService.updateHospitalDepartment(hospitalDepartmentBean);
				msg = "修改用户【" + hospitalDepartmentBean.getName() + "】成功";
		} catch (LogicException e) {
			result = false;
			msg = e.getMessage();
			
		} catch (Exception e) {
			result = false;
			msg = "修改用户出错，系统内部错误";
		}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("result", result);
			map.put("msg", msg);

			return map;
		}
		/***
		 * 删除
		 * <功能详细描述>
		 * @param hospitalDepartmentBean
		 * @return
		 * @see [类、类#方法、类#成员]
		 */
		@RequestMapping("/delete")
		@ResponseBody
		public Map<String , Object> delete(String id){
			boolean result = true;
			String msg = "";
			HospitalDepartmentBean hdtment =new HospitalDepartmentBean();
			hdtment.setId(id);
			try {
				this.hospitalDepartmentService.delHospitalDepartment(hdtment);
				msg = "删除用户成功";
			} catch (LogicException e) {
				result = false;
				msg = e.getMessage();
			} catch (Exception e) {
				result = false;
				msg = "删除用户出错，系统内部错误";
			}

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("result", result);
			map.put("msg", msg);

			return map;
			
		}
		
		/***
		 * 根据医院查询对应科室
		 * <功能详细描述>
		 * @param doctorBean
		 * @return
		 * @see [类、类#方法、类#成员]
		 */
		@RequestMapping("/grade/next")
		@ResponseBody
		public List<HospitalDepartmentBean> queryNextGradeLocation(HospitalDepartmentBean hospitalDepartmentBean) {

			return hospitalDepartmentService.queryHospitalDepartment(hospitalDepartmentBean);
		}
}
